package com.linkage.event.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.exception.ServiceException;
import com.linkage.event.pojo.dao.UserLocationMapper;
import com.linkage.event.pojo.entity.UserLocation;
import com.linkage.event.service.UserLocationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserLocationServiceImpl implements UserLocationService {

    @Resource
    private UserLocationMapper userLocationMapper;

    @Override
    public void addUserLocations(List<UserLocation> list) {
        list.forEach(x ->{
            if(StrUtil.isEmpty(x.getCreateBy())){
                throw new ServiceException(500,"创建人为空");
            }
//            if(StrUtil.isEmpty(x.getUserCode())){
//                throw new ServiceException(500,"用户code为空");
//            }
            if(StrUtil.isEmpty(x.getUserRange())){
                throw new ServiceException(500,"围栏信息为空");
            }
            if(StrUtil.isEmpty(x.getRangeName())){
                throw new ServiceException(500,"围栏名称为空");
            }
            x.setCode(UUID.fastUUID().toString(true));
            x.setCreateTime(new Date());
            x.setUpdateTime(new Date());
        });
        userLocationMapper.addBatch(list);
    }

    @Override
    public void updateUserLocation(UserLocation userLocation) {
        userLocationMapper.updateByPrimaryKeySelective(userLocation);
    }

    @Override
    public List<UserLocation> queryLocationByCreate(String create) {
        return userLocationMapper.queryByCreate(create);
    }

    @Override
    public void delUserLocation(UserLocation userLocation){
        userLocationMapper.deleteLocation(userLocation);
    }
}
